package com.atguigu.guli.serviceucenter.service.impl;

import com.atguigu.commonutils.utils.JwtUtil;
import com.atguigu.commonutils.utils.MD5Util;
import com.atguigu.guli.serviceucenter.entity.Member;
import com.atguigu.guli.serviceucenter.entity.vo.LoginVo;
import com.atguigu.guli.serviceucenter.entity.vo.RegisterVo;
import com.atguigu.guli.serviceucenter.mapper.MemberMapper;
import com.atguigu.guli.serviceucenter.service.MemberService;
import com.atguigu.servicebase.exception.Assert;
import com.atguigu.servicebase.exception.ServiceException;
import com.atguigu.servicebase.result.ResponseEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 会员表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-08-05
 */
@Service
public class MemberServiceImpl extends ServiceImpl<MemberMapper, Member> implements MemberService {


    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    @Override
    public String login(LoginVo loginVo) {
        String mobile = loginVo.getMobile();
        String password = loginVo.getPassword();

        //校验参数
        Assert.notEmpty(mobile, ResponseEnum.PHONE_EMPTY);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_EMPTY);

        //获取会员
        Member member = baseMapper.selectOne(new QueryWrapper<Member>().eq("mobile", mobile));
        Assert.notNull(member, ResponseEnum.MEMBER_NULL);

        //校验密码
        if(!MD5Util.encrypt(password).equals(member.getPassword())) {
            throw new ServiceException(ResponseEnum.PASSWORD_ERROR);
        }

        //校验是否被禁用
        if(member.getDisabled()) {
            throw new ServiceException(ResponseEnum.MEMBER_DESABLE);
        }

        //使用JWT生成token字符串
        String token = JwtUtil.getJwtToken(member.getId(), member.getNickname());
        return token;
    }

    @Override
    public void register(RegisterVo registerVo) {

        //获取注册信息，进行校验
        String nickname = registerVo.getNickname();
        String mobile = registerVo.getMobile();
        String password = registerVo.getPassword();
        String code = registerVo.getCode();

        //校验参数
        Assert.notEmpty(mobile, ResponseEnum.PHONE_EMPTY);
        Assert.notEmpty(password, ResponseEnum.PASSWORD_EMPTY);
        Assert.notEmpty(code, ResponseEnum.CODE_EMPTY);

        //校验校验验证码
        //从redis获取发送的验证码
        String mobleCode = redisTemplate.opsForValue().get(mobile);
        if(!code.equals(mobleCode)) {
            throw new ServiceException(ResponseEnum.CODE_ERROR);
        }
        //查询数据库中是否存在相同的手机号码
        Integer count = baseMapper.selectCount(new QueryWrapper<Member>().eq("mobile", mobile));
        if(count.intValue() > 0) {
            throw new ServiceException(ResponseEnum.MEMBER_EXIST);
        }

        //添加注册信息到数据库
        Member member = new Member();
        member.setNickname(nickname);
        member.setMobile(mobile);
        member.setPassword(MD5Util.encrypt(password));
        member.setDisabled(false);
        member.setAvatar("http://thirdwx.qlogo.cn/mmopen/vi_32/DYAIOgq83eoj0hHXhgJNOTSOFsS4uZs8x1ConecaVOB8eIl115xmJZcT4oCicvia7wMEufibKtTLqiaJeanU2Lpg3w/132");
        this.save(member);
    }

}
